package com.dnjn.happiness.cms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.cms.domain.Article;
import com.dnjn.happiness.cms.service.IArticleService;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.core.utils.StringUtils;
import com.dnjn.happiness.common.core.utils.poi.ExcelUtil;
import com.dnjn.happiness.common.core.web.page.TableDataInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import com.dnjn.happiness.common.security.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * 文章管理Controller
 *
 * @author amber
 * @date 2023-10-10
 */
@Tag(name = "cms Article", description = "Banner")
@RestController
@RequestMapping("/article")
public class ArticleController extends BaseController {
    @Autowired
    private IArticleService articleService;

    @Data
    public static class ArticleVo implements Serializable {
        private Long articleId;
        private String articleTitle;
        private String articleContent;
    }

    @GetMapping(value = {"/getInfoWx/{articleId}"})
    public AjaxResult getInfoWx(@PathVariable("articleId") Long articleId) {
        return success(articleService.selectArticleByArticleId(articleId));
    }

    @RequiresPermissions("cms:article:list")
    @PostMapping("/page")
    public AjaxResult page(@RequestBody Article domain, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page page = Page.of(current, size).addOrder(OrderItem.desc("article_id"));
        Page<Article> list = articleService.page(page, new QueryWrapper<Article>()
                .eq(StringUtils.isNotEmpty(domain.getShowable()), "showable", domain.getShowable())
                .eq(StringUtils.isNotEmpty(domain.getDeleted()), "deleted", domain.getDeleted())
                .like(StringUtils.isNotEmpty(domain.getArticleTitle()), "article_title", domain.getArticleTitle()));
        return success(list);
    }


    @PostMapping("/getPage")
    public AjaxResult getPage(@RequestBody Article domain, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page page = Page.of(current, size);
        Page<Article> list = articleService.page(page, new QueryWrapper<Article>()
                .eq("deleted", "0")
                .eq(StringUtils.isNotEmpty(domain.getShowable()), "showable", domain.getShowable())
                .like(StringUtils.isNotEmpty(domain.getArticleTitle()), "article_title", domain.getArticleTitle()));
        return success(list);
    }

    /**
     * 查询文章管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Article article) {
        startPage();
        List<Article> list = articleService.selectArticleList(article);
        return getDataTable(list);
    }

    /**
     * 导出文章管理列表
     */
    @RequiresPermissions("cms:article:export")
    //@Log(title = "文章管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Article article) {
        List<Article> list = articleService.selectArticleList(article);
        ExcelUtil<Article> util = new ExcelUtil<Article>(Article.class);
        util.exportExcel(response, list, "文章管理数据");
    }

    /**
     * 获取文章管理详细信息
     */
    @RequiresPermissions("cms:article:query")
    @GetMapping(value = {"/get/{articleId}"})
    public AjaxResult get(@PathVariable("articleId") Long articleId) {
        return success(articleService.selectArticleByArticleId(articleId));
    }




    /**
     * 新增文章管理
     */
    @RequiresPermissions("cms:article:add")
    //@Log(title = "文章管理", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody Article article) {
        validLinkedUrl(article.getCoverUrl());
        return toAjax(articleService.save(article));
//        return toAjax(articleService.insertArticle(article));
    }

    /**
     * 修改文章管理
     */
    @RequiresPermissions("cms:article:edit")
    //@Log(title = "文章管理", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Article article) {
        validLinkedUrl(article.getCoverUrl());

        return toAjax(articleService.updateArticle(article));
    }



    /**
     * 删除文章管理
     */
    @RequiresPermissions("cms:article:remove")
    //@Log(title = "文章管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{articleIds}")
    public AjaxResult remove(@PathVariable Long[] articleIds) {
//        return toAjax(articleService.deleteArticleByArticleIds(articleIds));
        return toAjax(articleService.updateArticleDeleteByArticleIds(articleIds));
    }

    /**
     * 小程序查询展示首页的文章
     */
    @GetMapping("/getIndexShowArticle")
    public AjaxResult getIndexShowArticle() {
        List<Article> articleList = articleService.lambdaQuery().eq(Article::getShowable, "Y").eq(Article::getDeleted, "0").list();
        return success(articleList);
    }
}
